<template>
  <div class="post-list">
    <article
      v-for="post in posts"
      :key="post._id"
      class="card mb-3 shadow-sm"
      @click="goPostDetail(post._id as string)"
    >
      <div class="card-body">
        <h4>
          {{ post.title }}
        </h4>
        <div class="row my-3 align-items-center">
          <div v-if="post.image" class="col-4">
            <img
              :src="post.image.fitUrl"
              :alt="post.title"
              class="rounded-lg w-100"
            />
          </div>
          <p :class="{ 'col-8': post.image }" class="text-muted">
            {{ post.excerpt }}
          </p>
        </div>
        <span class="text-muted">{{ post.createdAt }}</span>
      </div>
    </article>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, computed } from "vue";
import { PostProps } from "@/store/index";
import { useRouter } from "vue-router";
import { generateFitUrl } from "@/util/helper";
import { ImageProps } from "@/store/index";
export default defineComponent({
  props: {
    list: {
      required: true,
      type: Array as PropType<PostProps[]>,
    },
  },
  setup(props) {
    const router = useRouter();
    const goPostDetail = (id: string) => {
      router.push("/post/" + id);
    };
    const posts = computed(() => {
      return props.list.map((post) => {
        generateFitUrl(post.image as ImageProps, 200, 110, ["m_fill"]);
        return post;
      });
    });
    return {
      goPostDetail,
      posts,
    };
  },
});
</script>

<style scoped>
.post-list article {
  cursor: pointer;
}
.post-list h4 a {
  text-decoration: none;
  color: #1a1a1a;
}

.post-list h4 a:hover {
  color: #0d6efd;
}
</style>
